package com.basker.pisces.core.meta;

import org.springframework.util.Assert;

public class ObjectMetaNameHelper {

    private static final String PART_META_NAME_SPLITOR1 = "@";
    private static final String PART_META_NAME_SPLITOR2 = "|";

    /**
     * 生成子集Meta的名称,
     *
     * @param orginalName
     * @param fields
     * @return
     */
    public static String generatePartMetaName(String orginalName, String[] fields) {
        Assert.hasText(orginalName, "parameter 'orginalName' is required");
        Assert.notEmpty(fields, "parameter 'fields' is required");

        return orginalName + PART_META_NAME_SPLITOR1 + String.join(PART_META_NAME_SPLITOR2, fields);
    }

    /**
     * 解析元数据名称，尝试获取子集Meta加载的字段
     *
     * @param metaName
     * @return
     */
    public static String[] getPartMetaFields(String metaName) {
        if (ObjectMetaNameHelper.isPartMetaName(metaName)) {
            return ObjectMetaNameHelper.splitPartMetaName(metaName)[1].split("\\" + PART_META_NAME_SPLITOR2);
        } else {
            return new String[0];
        }
    }

    public static boolean isPartMetaName(String metaName) {
        Assert.hasText(metaName, "parameter 'metaName' is required");
        return metaName.contains(PART_META_NAME_SPLITOR1);
    }

    public static String[] splitPartMetaName(String metaName) {
        return metaName.split(PART_META_NAME_SPLITOR1);
    }

}
